﻿package  {
	import fl.controls.Slider;
	import fl.events.SliderEvent;
	import flare.analytics.optimization.AspectRatioBanker;
	import flare.animate.Transitioner;
	import flare.animate.Tween;
	import flare.data.DataSet;
	import flare.data.DataTable;
	import flare.display.LineSprite;
	import flare.scale.ScaleType;
	import flare.vis.data.Data;
	import flare.vis.data.DataSprite;
	import flare.vis.data.render.EdgeRenderer;
	import flare.vis.operator.encoder.ColorEncoder;
	import flare.vis.operator.encoder.PropertyEncoder;
	import flare.vis.operator.encoder.ShapeEncoder;
	import flare.vis.operator.filter.VisibilityFilter;
	import flare.vis.operator.layout.AxisLayout;
	import flare.util.Shapes;
	import flare.vis.operator.OperatorList;
	import flare.vis.Visualization;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	/**
	* ...
	* @author Default
	*/
	public class SampeFlare extends Sprite{
		
		private var container:Sprite;
		private var vis:Visualization;
		private var d:Data;
		private var sldBGE:Slider;
		private var sldMwst:Slider;
		private var txtBge:TextField;
		private var txtMwst:TextField;
		private var ax:AxisLayout;
		
		
		public function SampeFlare() {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			init();
			
		}
		
		private function init():void {
			
			var sld:Slider = new Slider();
			sld.minimum = 200;
			sld.maximum = 5000;
			sld.addEventListener(SliderEvent.CHANGE, slider_change);
			sld.x = 10;
			sld.liveDragging = true;
			sldBGE = sld;
			sld.width = 200;
			
			var txt:TextField = new TextField();
			txt.x = sld.x + sld.width + 10;
			txt.y = sld.y;
			txtBge = txt;
			
			sld = new Slider();
			sld.minimum = 10;
			sld.maximum = 300;
			sld.addEventListener(SliderEvent.CHANGE, slider_change);
			sld.x = 10;
			sld.y = 30;
			sld.liveDragging = true;
			sldMwst = sld;
			sld.width = 200;
			
			txt = new TextField();
			txt.x = sld.x + sld.width + 10;
			txt.y = sld.y;
			txtMwst = txt;
			
			
			container = new Sprite();
			container.graphics.beginFill(0);
			container.graphics.drawRect(0, 0, 100, 30);
			container.graphics.endFill();
			
			container.x = 300;
			container.y = 300;
			
			//d.createEdges("data.id","data.sales");
			var bounds:Rectangle= new Rectangle(0, 0, 600, 400);
			d = getTimeline(20, 1);
			
			var timeline:OperatorList = new OperatorList(
				// the banker automatically selects the visualization
				// bounds to optimize the perception of trends in the chart
				//new AspectRatioBanker("data.progression", true, bounds.width, bounds.height),
				//new AspectRatioBanker("data.consum",true,bounds.width, bounds.height),
				new VisibilityFilter(actualFilter),
				ax=new AxisLayout("data.consum", "data.progression",false,false),
				new ColorEncoder("data.series", Data.EDGES,	"lineColor", ScaleType.CATEGORIES),
				//new ColorEncoder("data.series", Data.NODES,
				//	"fillColor", ScaleType.CATEGORIES),
				new PropertyEncoder({
					lineAlpha: 0, alpha: 0, buttonMode: false,
					scaleX: 1, scaleY: 1, size: 0.5
				}),
				new PropertyEncoder({lineWidth:2}, Data.EDGES)
			);
			
			
			
			vis = new Visualization(d);
			vis.bounds = bounds.clone();
			vis.y = 100;
			
			
            vis.x = 100;
            vis.y = 50;
            
			
			vis.operators.add(timeline);
			
			ax.yScale.max = 50;
			ax.yScale.min = -50;
			
            vis.update();
			
			addChild(vis);
			addChild(txtBge);
			addChild(txtMwst);
			addChild(sldBGE);
			addChild(sldMwst);
			
			stage.addEventListener(MouseEvent.CLICK, stage_click);
		}
		private function actualFilter(da:DataSprite):Boolean {
			return true;
		}
		private function slider_change(e:SliderEvent):void {
			d.visit(function(da:DataSprite):void {
				if (da.data.series == 1)
					da.data.progression = Math.min(Math.max(calcProgression(da.data.consum) * 100, -sldMwst.value), sldMwst.value);
			});
			
			
			
			ax.yScale.max = sldMwst.value;
			ax.yScale.min = -sldMwst.value;
			txtBge.text = sldBGE.value.toString();
			txtMwst.text = sldMwst.value + "%";
			
			vis.update();
		}
		private function calcProgression(a:Number):Number {
			return ((a * sldMwst.value/100) - sldBGE.value) / a;
		}
		
		private function stage_click(e:MouseEvent):void {
			var da:DataSprite;
			
			
			
			vis.update(new Transitioner(0.5)).play();
		}
		
		public function getTimeline(N:int, M:int):Data
		{
			var data:Data = new Data();
			var fx:Number;
			var year:Number;
			for (var i:int = 4; i < 100; i++) {
				//A * S 
				fx = ((i * 100 * 0.5) - 1500) / (i * 100);
				data.addNode( {
					series:1,
					consum:i * 100,
					progression: Math.min(Math.max(fx * 100, -50), 50)
					});
					trace("(SampeFlare.getTimeline) " + fx);
				year = i * 100 * 14;
				data.addNode( { 
					series:2,
					consum:i*100,
					progression:(Math.min(15000, Math.max(0, year - 10000)) * 0.38 + Math.min(25000, Math.max(0, year - 25000)) * 0.43 + Math.max(0, year - 51000) * 0.5)/year*100
				} );
			}
			data.createEdges("data.consum", "data.series");
			
			
			return data;
			/*
			var MAX:Number = 60;
			var t0:Date = new Date(1979,5,15);
			var t1:Date = new Date(1982,2,19);
			var x:Number, f:Number;
			
			var data:Data = new Data();
			//for (var j:uint=0; j<M; ++j) {
				for (var i:uint=0; i<N; ++i) {
					f = i/(N-1);
					x = t0.time + f*(t1.time - t0.time);
					data.addNode({
						series: 1,
						date: new Date(x),
						count:Math.random() * 40
					});
				}
			//}
			// create timeline edges connecting items sorted by date
			// and grouped by series
			data.createEdges("data.date");

			return data;
			*/
		}
		
	}
	
}